package com.mego.weblink.activitys;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;

public class BootBroadcastReceiver extends BroadcastReceiver {
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        log("BootBroadcastReceiver--1\n");
        if (intent.getAction().equals(ACTION)) {
            log("BootBroadcastReceiver--2\n");
            Intent mainActivityIntent = new Intent(context, WelcomeActivity.class);  // 要啟動的Activity
            mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(mainActivityIntent);
        }
    }

    private FileOutputStream mFout = null;

    public void log(String message) {

        String RUNDIR = Environment.getExternalStorageDirectory() + "/";
        message += "\n";
        if (mFout == null) {
            try {
                File file = new File(RUNDIR + "log.txt");
                if (file.exists())
                    file.delete();
                file.createNewFile();
                mFout = new FileOutputStream(RUNDIR + "log.txt");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        try {
            byte[] bytes = message.getBytes();
            mFout.write(bytes);
            mFout.flush();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
